from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.


class UserInfo(AbstractUser):
    phone = models.BigIntegerField(verbose_name='手机号', null=True, blank=True)
    # null=True 告诉数据库数据可以为空 但是admin后台管理不许为空
    # 头像
    avatar = models.FileField(upload_to='avatar/', default='avatar/default.png', verbose_name='用户头像')
    create_time = models.DateField(auto_now_add=True)

    blog = models.OneToOneField(to='Blog', on_delete=models.CASCADE, null=True)

    class Meta:
        verbose_name_plural = '用户表'  # 修改admin后台管理默认的表名 不会加s
        # verbose_name = '用户表'  # 末尾还是会加s

    def __str__(self):
        return self.username


# 站点
class Blog(models.Model):
    site_name = models.CharField(verbose_name='站点名称', max_length=32)
    site_title = models.CharField(verbose_name='站点标题', max_length=32)
    site_theme = models.CharField(verbose_name='站点样式', max_length=64)  # 存css/js文件路径

    def __str__(self):
        return self.site_name


# 文章分类
class Category(models.Model):
    name = models.CharField(verbose_name='文章分类', max_length=32)

    def __str__(self):
        return self.name

    # 一个站点下有多个文章分类
    blog = models.ForeignKey(to='Blog', null=True, on_delete=models.CASCADE)


# 文章标签
class Tag(models.Model):
    name = models.CharField(verbose_name='文章标签', max_length=32)

    blog = models.ForeignKey(to='Blog', null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(verbose_name='文章标题', max_length=64)
    desc = models.CharField(verbose_name='文章简介', max_length=255)
    content = models.TextField(verbose_name='文章内容')
    create_time = models.DateField(auto_now_add=True)

    # 数据库字段设计优化
    up_num = models.BigIntegerField(default=0, verbose_name='点赞数')
    down_num = models.BigIntegerField(default=0, verbose_name='点踩数')
    comment_num = models.BigIntegerField(verbose_name='评论数', default=0)

    # 外键字段
    blog = models.ForeignKey(to='Blog', null=True, on_delete=True)
    category = models.ForeignKey(to='Category', null=True, on_delete=models.CASCADE)

    tags = models.ManyToManyField(to='Tag', through='Article2Tag', through_fields=('article', 'tag'))

    def __str__(self):
        return self.title


class Article2Tag(models.Model):
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    tag = models.ForeignKey(to='Tag', on_delete=models.CASCADE)


class UpAndDown(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    is_up = models.BooleanField()


class Comment(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    content = models.CharField(max_length=255, verbose_name='评论内容')
    comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')

    # 自关联
    parent = models.ForeignKey(to='self', null=True, on_delete=models.CASCADE)


